﻿//Cart.aspx.cs
////////////////////////////////////////
////////////////////////////////////////
//点击首页的“我的购物车”或点击商品旁边的加入购物车按钮时显示，显示用户的购物车
////////////////////////////////////////
////////////////////////////////////////
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

public partial class Cart : System.Web.UI.Page
{
    CommonClass ccObj = new CommonClass();
    float sum = 0;     //乐器总额
    protected void Page_Load(object sender, EventArgs e)
    {
        //判断Session对象中是否存在用户已购买商品的列表。如果存在则取出，并调用自定义方法将列表绑定到GridViewk中。
        if (Session["OrderList"] != null)
        {
            BindShoppingCart();
        }
        else
        {
            Response.Write(ccObj.MessageBox("购物车中没有商品！","Default.aspx"));
            this.CartView.Visible = false;
            this.Continueshop.Visible = false;
            this.Summitdeliv.Visible = false;
            this.Canceldeliv.Visible = false;
        }
    }
    private void BindShoppingCart()
    {
        ArrayList orderArrayList = (ArrayList)Session["OrderList"];
        //定义数据表dtCart并添加其数据列
        DataTable dtCart = new DataTable();
        dtCart.Columns.Add(new DataColumn("No"));
        dtCart.Columns.Add(new DataColumn("instruID"));
        dtCart.Columns.Add(new DataColumn("name"));
        dtCart.Columns.Add(new DataColumn("sellPrice",System.Type.GetType("System.Decimal")));
        dtCart.Columns.Add(new DataColumn("quantity",System.Type.GetType("System.Int32")));
        dtCart.Columns.Add(new DataColumn("totalPrice", System.Type.GetType("System.Decimal"),"sellPrice * quantity"));

        for (int i = 0; i < orderArrayList.Count; i++)
        {
            //从保存用户已购买商品的列表对象中取出数据，添加到表dtCart中。
            OrderList orderlist = (OrderList)orderArrayList[i];
            DataRow row = dtCart.NewRow();
            
            row["No"] = i + 1;
            row["instruID"] = orderlist.instruID;
            row["name"] = orderlist.goodname;
            row["sellPrice"] = orderlist.goodprice;
            row["quantity"] = orderlist.quantity;
            dtCart.Rows.Add(row);

            sum += float.Parse(row["totalPrice"].ToString());   //计算商品总价格
        }
        //将dtCart表绑定到GridView控件中
        this.CartView.DataSource = dtCart;
        this.CartView.Columns[4].FooterText = sum.ToString();   //将商品总计通过GridView控件“小计”列的FooterText属性显示
        this.CartView.DataBind();
    }
    protected void btnContinueshop_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }
    protected void btnSummitdeliv_Click(object sender, EventArgs e)
    {
        Response.Redirect("SummitDelivery.aspx?sum="+sum);
    }
    protected void btnCanceldeliv_Click(object sender, EventArgs e)
    {
        Session["OrderList"] = null;
        Response.Redirect(ccObj.MessageBox("订单已取消！","Default.aspx"));
    }
    protected void lnkbtnDelete_Command(object sender, CommandEventArgs e)
    {
        int instruID = (int)e.CommandArgument;
        ArrayList orderArrayList = (ArrayList)Session["OrderList"];//获取其购物车
        //从ArrayList中，将指定的商品从购物车中移除，其中，删除按钮(lnkbtnDelete)的CommandArgument参数值为商品ID代号
        //////////////////////////////////
        //////////////////////////////////
        //将ID号为e.CommandArgument的去掉。
        //////////////////////////////////
        //////////////////////////////////
        //////////////////////////////////
        //////////////////////////////////
        for (int i = 0; i < orderArrayList.Count; i++)
        {
            OrderList orderlist = (OrderList)orderArrayList[i];
            if (orderlist.instruID == instruID)
            {
                orderArrayList.RemoveAt(i);
                break;
            }
        }
        Session["OrderList"] = orderArrayList; //更新购物车
        Response.Redirect("Cart.aspx");
    }
    protected void CartView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        CartView.PageIndex = e.NewPageIndex;
        this.BindShoppingCart();
    }
    protected void txtNum_TextChanged(object sender, EventArgs e)
    {/*
        ArrayList orderArrayList = (ArrayList)Session["OrderList"];//获取其购物车
        foreach (GridViewRow gvr in this.CartView.Rows)
        {
            TextBox otb = (TextBox)gvr.FindControl("txtNum"); //找到用来输入数量的TextBox控件 
            int count = Int32.Parse(otb.Text);//获得用户输入的数量值
            string instruID;//////////////////////////得到该商品的ID代
            //更新ID为instruID的商品的数量。
        
        }
        Session["OrderList"] = orderArrayList;//更新购物车
        this.BindShoppingCart();*/
    }
}
